#include "cairx.h"
#include "v2/xml/xml_device_object.h"

CAirx::CAirx(QObject *parent) :
    CKnxControl(parent)
{
    _mode_id = -1;
    _wind_id = -1;
    _curent_temp = 16;
}

/*

selection="16,29;0,1,2,3,4,5;0,1,2,3,4",以分号隔开3个类别,分别是
温度最小值,温度最大值;模式枚举;风速枚举

枚举值:
风速: 0,1,2,3,4,5,6		0自动 1~6 档
模式: 0,1,2,3,4,5,6,7		0自动 1热 2冷 3通风 4除湿 5换气 6睡眠 7清爽

*/

void CAirx::parseExp(QString exp)
{
}

void CAirx::parseSelection(QString selection)
{
    //表达式状态
    _onValue = "1"; _offValue = "0";
    _min = 16; _max = 30;

    QStringList list = selection.split(";");
    if(list.size() >=3 )
    {
        QStringList mmlist = list.at(0).split(",");
        if(mmlist.size() == 2)
        {
            _min = mmlist.at(0).toInt();
            _max = mmlist.at(1).toInt();
        }

        _selectionWindArray = list.at(1).split(",");
        _selectionModeArray = list.at(2).split(",");
    }
}

void CAirx::setKnxObject(void *xOBJECT)
{
    Xml_DEVICE_OBJECT *object = (Xml_DEVICE_OBJECT *)xOBJECT;
    //发送值
    _sendOnArray = object->_send.split(",");
    _sendValueArray = object->_dim_send.split(",");
    _sendModeArray = object->_mode_send.split(",");
    _sendWindArray = object->_wind_send.split(",");

    //反馈值
    QStringList arrys = object->_status.split(",");
    if(arrys.size()>=3)
    {
        QStringList feedList = arrys.at(0).split(":");
        if(feedList.size() >= 3)
        {
            _feedbackAddress = arrys.at(0);
            _feedbackDataType =  (DATA_TYPE)feedList.at(1).toUInt();
            _feedbackValueDefine =  (DATA_DEFINE)feedList.at(2).toUInt();
        }

        feedList = arrys.at(1).split(":");
        if(feedList.size() >= 3)
        {
           _feedbackDimAddress = arrys.at(1);
           _feedbackDimType =  (DATA_TYPE)feedList.at(1).toUInt();
           _feedbackDimDefine =  (DATA_DEFINE)feedList.at(2).toUInt();
        }

        feedList = arrys.at(2).split(":");
        if(feedList.size() >= 3)
        {
            _feedbackWindAddress = arrys.at(2);
            _feedbackWindType =  (DATA_TYPE)feedList.at(1).toUInt();
            _feedbackWindDefine =  (DATA_DEFINE)feedList.at(2).toUInt();
        }

        feedList = arrys.at(3).split(":");
        if(feedList.size() >= 4)
        {
            _feedbackModeAddress = arrys.at(3);
            _feedbackModeType =  (DATA_TYPE)feedList.at(1).toUInt();
            _feedbackModeDefine =  (DATA_DEFINE)feedList.at(2).toUInt();
        }
    }
}

QString CAirx::getWindOrModeDefineIndex_ByStatusValue(QString value,QString status,QStringList defines)
{
    QStringList status_define = status.split(":");
    if(status_define.size()>3)
    {
        QStringList status_values = status_define.at(3).split("|");
        int index = status_values.indexOf(value);
        if(index>=0 && index<defines.size())
        {
            return defines.at(index);
        }
    }

    qDebug() << "getWindOrModeDefineIndex_ByStatusValue: not found "
             << value << " @ " << status << " @ " << defines;

    return value;
}

QString CAirx::valueCheck(QString action, QString value)
{
    if(action == DEVICE_ACTION_SWITCH)
    {
        return value;
    }
    else if(action == DEVICE_ACTION_TEMP)
    {
        float nv = value.toFloat();
        if(nv<_min)
            nv = _min;

        if(nv>_max)
            nv = _max;

        _curent_temp = (int)nv;
        return QString("%1").arg(nv);
    }
    else if(action == DEVICE_ACTION_WIND)
    {
        return getWindOrModeDefineIndex_ByStatusValue(value,_feedbackWindAddress,_selectionWindArray);
    }
    else if(action == DEVICE_ACTION_MODE)
    {
        return getWindOrModeDefineIndex_ByStatusValue(value,_feedbackModeAddress,_selectionModeArray);
    }

    qDebug() << "CAirx valueCheck: not found "
             << value << " @ " << action ;

    return value;
}

void CAirx::readKnxAction()
{
    CKnxExecThread *eThread = new CKnxExecThread();
    eThread->ttgConnector = (CTTGIpsConnector *)cntDelegate;

    connect(eThread,SIGNAL(finished()),eThread,SLOT(deleteLater()));

    CExecStruct *s = new CExecStruct();
    s->address = _feedbackAddress.split(":")[0];
    eThread->execList.append(s);

    s = new CExecStruct();
    s->address = _feedbackDimAddress.split(":")[0];
    eThread->execList.append(s);

    s = new CExecStruct();
    s->address = _feedbackModeAddress.split(":")[0];
    eThread->execList.append(s);

    s = new CExecStruct();
    s->address = _feedbackWindAddress.split(":")[0];
    eThread->execList.append(s);

    eThread->start();
}

void CAirx::execKnxAction(QString action,QString value)
{
    qDebug() << "CAirx::execKnxAction " << action << "=" << value;

    CKnxExecThread *eThread = new CKnxExecThread();
    eThread->ttgConnector = (CTTGIpsConnector *)cntDelegate;

    connect(eThread,SIGNAL(finished()),eThread,SLOT(deleteLater()));

    QStringList *sendArray = &_sendOnArray;
    QString feedback = _feedbackAddress;

    if (action == DEVICE_ACTION_SWITCH) {
        sendArray = &_sendOnArray;
        feedback = _feedbackAddress;
    }
    else if(action == DEVICE_ACTION_TEMP) {
        sendArray = &_sendValueArray;
        feedback = _feedbackDimAddress;
    }
    else if(action == DEVICE_ACTION_MODE) {
        sendArray = &_sendModeArray;
        feedback = _feedbackModeAddress;
    }
    else if(action == DEVICE_ACTION_WIND) {
        sendArray = &_sendWindArray;
        feedback = _feedbackWindAddress;
    }

    QString sendAddress,sendValue;
    DATA_TYPE sendDataType;
    DATA_DEFINE sendValueDefine;

    QStringList feedbackStatusList = feedback.split(":");

    for (int i=0; i<sendArray->size(); i++)
    {
        QString dev = sendArray->at(i);
        QStringList devArray = dev.split(":");

        if (devArray.size() >= 3 && feedbackStatusList.size()>=3) {

            sendAddress = devArray.at(0);

            if (sendAddress == "msleep") {
                CExecStruct *s = new CExecStruct();
                s->address = dev;

                eThread->execList.append(s);
            }
            else
            {

                sendDataType = (DATA_TYPE)devArray.at(1).toUInt();
                sendValueDefine = (DATA_DEFINE)devArray.at(2).toUInt();
                QString fv = feedbackStatusList.at(3);

                if (action == DEVICE_ACTION_SWITCH) {
                    QStringList sl = fv.split("|");
                    if(sl.size() == 2)
                    {
                        if(value == DEVICE_STATUS_ON)
                        {
                            sendValue = sl.at(0);
                        }else{
                            sendValue = sl.at(1);
                        }
                    }else{
                        sendValue = fv;
                    }
                }
                else if(action == DEVICE_ACTION_TEMP ) {
                    sendValue = value;
                }
                else if(action == DEVICE_ACTION_MODE )
                {
                    QStringList ml = fv.split("|");
                    int index = _selectionModeArray.indexOf(value);
                    if(index>=0 && index < ml.size())
                    {
                        sendValue = ml.at(index);
                    }else{
                        qDebug() << "CAirx: mode could not found index " << index << "@" << _selectionModeArray;
                        sendValue = value;
                    }
                }
                else if(action == DEVICE_ACTION_WIND )
                {
                    QStringList ml = fv.split("|");
                    int index = _selectionWindArray.indexOf(value);
                    if(index>=0 && index < ml.size())
                    {
                        sendValue = ml.at(index);
                    }else{
                        qDebug() << "CAirx: wind could not found index " << index << "@" << _selectionWindArray;
                        sendValue = value;
                    }
                }

                if(!sendAddress.isEmpty())
                {
                    qDebug() << "CAirx:" <<  action << " = " << sendValue;

                    int data_len = 0;
                    memset(_data_buf,0,sizeof(_data_buf));

                    if((data_len = DEVICE_OBJECT::KnxQStringToData(sendValue,sendDataType,sendValueDefine,(_u8 *)_data_buf)) == -1)
                    {
                        qDebug() << "SetDev ret='-1' msg='error data args'" ;
                        continue ;
                    }

                    CExecStruct *s = new CExecStruct();
                    s->address = sendAddress;
                    s->type = sendDataType;
                    s->data = QByteArray((const char *)_data_buf,data_len);

                    eThread->execList.append(s);
                }
            }
        }

    }

    eThread->start();
}

QString CAirx::actionFromStatus(QString address,QString value)
{
    if(_feedbackAddress.startsWith(address))
    {
        return DEVICE_ACTION_SWITCH;
    }
    else if(_feedbackDimAddress.startsWith(address))
    {
        return DEVICE_ACTION_TEMP;
    }
    else if(_feedbackModeAddress.startsWith(address))
    {
        return DEVICE_ACTION_MODE;
    }
    else if(_feedbackWindAddress.startsWith(address))
    {
        return DEVICE_ACTION_WIND;
    }

    return address;
}

void CAirx::execDeviceAction(QString action,QString value,QString unit)
{
    if(action == DEVICE_ACTION_SWITCH)
    {
        execKnxAction(DEVICE_ACTION_SWITCH,value);
    }
    else if(action == DEVICE_ACTION_TEMP)
    {
        int setv = 0;

        if(unit.startsWith("="))
        {
            _curent_temp = parseNumberFromString(value);
            execKnxAction(DEVICE_ACTION_TEMP,value);
        }
        else if(unit.startsWith("+"))
        {
            setv = parseNumberFromString(value);
            _curent_temp += setv;
            if(_curent_temp>_max)
                _curent_temp = _max;

            execKnxAction(DEVICE_ACTION_TEMP,QString("%1").arg(_curent_temp));
        }
        else if(unit.startsWith("-"))
        {
            setv = parseNumberFromString(value);
            _curent_temp -= setv;
            if(_curent_temp < _min)
                _curent_temp = _min;

            execKnxAction(DEVICE_ACTION_TEMP,QString("%1").arg(_curent_temp));
        }
    }
    else
    {
        execKnxAction(action,value);
    }


}
